package com.hgq.jssjyyxt.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hgq.jssjyyxt.entity.Bookings;
import com.hgq.jssjyyxt.mapper.BookingsMapper;
import com.hgq.jssjyyxt.mapper.CoursesMapper;
import com.hgq.jssjyyxt.entity.Courses;
import com.hgq.jssjyyxt.service.CoursesService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.hgq.jssjyyxt.util.Result;

import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * (Courses)表服务实现类
 *
 * @author makejava
 * @since 2024-04-11 13:53:37
 */
@Service("coursesService")
public class CoursesServiceImpl extends ServiceImpl<CoursesMapper, Courses> implements CoursesService {


    @Autowired
    private BookingsMapper bookingsMapper;

    @Override
    public Result selectAll(Courses courses, Integer limit, Integer page) {
        List<Map<String, Object>> list = baseMapper.selectAll(courses, (page - 1) * limit, limit);
        HashMap<String, Object> result = new HashMap<>();
        result.put("count", baseMapper.selectAllCount(courses));
        result.put("data", list);
        return Result.success(result);
    }

    @Override
    public Result findAllByUserId(Courses courses, Integer limit, Integer page) {
        List<Map<String, Object>> list = baseMapper.findAllByUserId(courses, (page - 1) * limit, limit);
        HashMap<String, Object> result = new HashMap<>();
        result.put("count", baseMapper.findAllCountByUserIdCount(courses));
        result.put("data", list);
        return Result.success(result);
    }

    @Override
    public Result findRandom() {
        return Result.success(baseMapper.findRandom());
    }

    @Override
    public Result editState(Courses courses) {
        //获取这个将要发布的课程信息
        List<Map<String, Object>> one = baseMapper.selectMaps(new QueryWrapper<Courses>().eq("course_id", courses.getCourseId()));
        if (one.isEmpty()) {
            return Result.failure("没有找到这个课程");
        }
        Map<String, Object> stringObjectMap = one.get(0);
        //获取状态  2改为发布状态 3停止状态
        Integer editState = courses.getState();
        if (editState == 2) {
            //查询这个教练已经发布的课程，判断时间是否冲突。
            List<Map<String, Object>> list = baseMapper.findAllByTrainerIdAndStateAndTime(stringObjectMap);
            if (!list.isEmpty()) {
                //获取所有冲突的课程标题
                StringBuilder sb = new StringBuilder();
                for (Map<String, Object> map : list) {
                    sb.append(map.get("title"));
                    sb.append(",");
                }
                sb.deleteCharAt(sb.length() - 1);
                return Result.failure("教练课程时间冲突，无法发布该课程！！！冲突课程：" + sb.toString());
            }
        } else if (editState == 3) {
            //判断这个课程是否已经有学员预约成功了
            QueryWrapper<Bookings> wrapper = new QueryWrapper<>();
            wrapper.eq("course_id", courses.getCourseId());
            wrapper.eq("status", 1);
            List<Bookings> bookingsList = bookingsMapper.selectList(wrapper);
            if (!bookingsList.isEmpty()) {
                //判断有没有到结束时间
                LocalDateTime endTime = (LocalDateTime) stringObjectMap.get("end_time");
                if (endTime.isAfter(LocalDateTime.now())) {
                    return Result.failure("还未到结束时间，还有学员，无法停止该课程");
                }
            }
        }

        if (baseMapper.updateById(courses) > 0) {
            return Result.success("修改成功");
        }
        return Result.failure("修改失败");
    }
}

